package dao.impl;

import dao.ClientDAO;
import database.UserData;
import database.utils.HibernateUtil;
import entity.Client;
import logger.LoggerManager;
import org.hibernate.Session;
import resurses.LoggerMessage;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alexander.makalov on 23.10.2014.
 */
public class ClientDAOImpl implements ClientDAO {
    @Override
    public void addClient(Client client) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(client);
            session.getTransaction().commit();
        } catch (Exception e) {
            LoggerManager.getLogger().Log(e.getMessage(), LoggerManager.LogLevel.ERROR);
            //e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void updateClient(Client client) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(client);
            session.getTransaction().commit();
        } catch (Exception e) {
            //e.printStackTrace();
            LoggerManager.getLogger().Log(e.getMessage(), LoggerManager.LogLevel.ERROR);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public Client getClientById(Long id) throws SQLException {
        Session session = null;
        Client client = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            client = (Client) session.load(Client.class, id);
        } catch (Exception e) {
            //e.printStackTrace();
            LoggerManager.getLogger().Log(e.getMessage(), LoggerManager.LogLevel.ERROR);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return client;
    }

    @Override
    public List getAllClients() throws SQLException {
        Session session = null;
        List<Client> clientList = new ArrayList<>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            clientList = session.createCriteria(Client.class).list();
        } catch (Exception e) {
            LoggerManager.getLogger().Log(e.getMessage(), LoggerManager.LogLevel.ERROR);
            //e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return clientList;
    }

    @Override
    public void saveAllClients(List<Client> clients) {
        Session session = null;
        try {
            if(UserData.getFileExist()){
                LoggerManager.getLogger().Log(LoggerMessage.BEGIN_TRANSACTION, LoggerManager.LogLevel.INFO);
                session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
                for(int i=0;i<clients.size();i++){
                    session.saveOrUpdate(clients.get(i));
                }
                LoggerManager.getLogger().Log(LoggerMessage.BEGIN_TRANSACTION_2, LoggerManager.LogLevel.INFO);
                session.getTransaction().commit();
                LoggerManager.getLogger().Log(LoggerMessage.TRANSACTION_END, LoggerManager.LogLevel.INFO);
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LoggerManager.getLogger().Log(e.getMessage() + " "+ e.getCause(), LoggerManager.LogLevel.ERROR);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void deleteClient(Client client) throws SQLException {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(client);
            session.getTransaction().commit();
        } catch (Exception e) {
            LoggerManager.getLogger().Log(e.getMessage(), LoggerManager.LogLevel.ERROR);
            //e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
}
